<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>for-in与for-of的使用</title>
    <style>
        ul{
            list-style: none;
            width: 200px;
            height: 90px;
            box-sizing: border-box;
        }
        li{
            border: 1px solid black;
            width: 100%;
            height: 30px;
            box-sizing: border-box;
        }
    </style>
</head>
<body>
    <ul>
        <li></li>
        <li></li>
        <li></li>
    </ul>
    <script>
        let num = ['a','b','c','d'];

        // for-in,键名
        for(let m in num){
            console.log(m,num[m]);
        }

        //for-of，键值
        for(let value of num){
            console.log(value);
        }




        let lesson = [
            {"name":"语文","teacher":"Ms Wang"},
            {"name":"数学","teacher":"Ms Li"},
            {"name":"英语","teacher":"Ms Zhao"}
        ];

        document.write(`
            <table border="1" width="100%">
            <thead><tr><td>课程名</td><td>任课老师</td></tr></thead>
        `)

        //使用for-in，键名 
        // for(let i in lesson){
        //     document.write(`<tr><td>${lesson[i].name}</td><td>${lesson[i].teacher}</td></tr>`)
        // }

        //使用for-of ，键值
        for(let detail of lesson){
            document.write(`<tr><td>${detail.name}</td><td>${detail.teacher}</td></tr>`)
        }

        document.write(`
            </table>
        `)





        // 迭代字符串
        let king = "xiasnn";
        for(let k of king){
            console.log(k);
        }





        //给dom元素添加事件
        let lis = document.querySelectorAll("li");
        
        for(let idx in lis){
            lis[idx].addEventListener('click',function () {
                lis[idx].style.backgroundColor = 'red';
            })
        }

        // for(let li of lis){
        //     li.addEventListener('click',function () {
        //         li.style.backgroundColor = 'red';
        //     })
        // }
    </script>
</body>
</html>